define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'paotui/posterapply/index' + location.search,
                    add_url: '',
                    edit_url: '',
                    del_url: 'paotui/posterapply/del',
                    multi_url: 'paotui/posterapply/multi',
                    import_url: 'paotui/posterapply/import',
                    table: 'paotui_poster_apply',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('姓名'), operate: 'LIKE'},
                        {field: 'mobile', title: __('手机号'), operate: 'LIKE'},
                        {field: 'schoolname', title: __('学校')},
                        {field: 'card', title: __('身份证正面'), operate: false,events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'card_back', title: __('身份证背面'), operate: false,events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'hand_card', title: __('手持身份证照片'), operate: false,events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'createtime', title: __('申请时间'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('状态'), searchList: {"0": __('待审核'),"1": '已通过'},formatter:Controller.api.formatter.status},

                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons: [
                                {
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if(row.status == 0 || row.status == -1){
                                            return true;
                                        }
                                        return false;
                                    },
                                    name: 'content',
                                    title: __('审核通过'),
                                    text:'审核通过',
                                    extend:'data-area=["600px","400px"]',
                                    classname: 'btn btn-xs btn-success btn-ajax',
                                    confirm:'确认审核通过吗？',
                                    success: function (data, ret) {
                                        $('#table').bootstrapTable('refresh');
                                    },
                                    url: 'paotui/posterapply/check'
                                },
                                {
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if(row.status == 0 || row.status == 1){
                                            return true;
                                        }
                                        return false;
                                    },
                                    name: 'detail',
                                    text: __('拒绝'),
                                    classname: 'btn btn-danger btn-xs btn-detail btn-dialog',
                                    url: 'paotui/posterapply/cancel',
                                    success: function (data, ret) {
                                        table.bootstrapTable('refresh');
                                    },
                                },
                            ],
                            formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            $(".nav-tabs a").click(function (){
                table.bootstrapTable('refresh', {url:$.fn.bootstrapTable.defaults.extend.index_url+'&status='+$(this).data('value')});
            })

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        cancel: function () {
            Controller.api.bindevent();
        },
        api: {
            formatter: {
                status: function (value, row, index) {
                    switch (value) {
                        case -1:
                            return "未通过";
                        case 1:
                            return "已通过";
                        case 0:
                            return "待审核";
                    }
                },
            },
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});